<template>
  <el-container>
    <el-main>
      <div class="changePassword_box">
        <el-form :rules="rules" :label-position="labelPosition" ref="form" :model="form" label-width="auto">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input type="password" v-model="form.oldPassword" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input type="password" v-model="form.newPassword" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="form.confirmPassword" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleModify">修改</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import {updateStudent} from "@/api/student";
import {updateTeacher} from "@/api/teacher";
import {updateAdministrator} from "@/api/administrator";

export default {
  name: "ChangePassword",
  data(){
    return{
      form:{},
      labelPosition:'left',
      rules: {
        oldPassword:[
          { required: true, message: '请输入旧密码', trigger: 'blur' },
        ],
        newPassword:[
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        confirmPassword:[
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    load(num) {
      if (num===1){
        this.form={};
      }else{
        this.form.newPassword='';
        this.form.confirmPassword='';
      }
    },
    handleModify(){
      this.$confirm('修改密码成功将退出登录，确定要修改密码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        let user = JSON.parse(sessionStorage.getItem("user"));
        this.$refs.form.validate(valid => {
          if (!valid) return;
          if (this.$md5(this.form.oldPassword)===user.password){
            if (this.form.newPassword===this.form.confirmPassword){
              if (this.form.oldPassword!==this.form.newPassword){
                user.password = this.form.newPassword;
                if (user.identity==="学生"){
                  updateStudent(user).then(res =>{
                    if (res.code===0){
                      sessionStorage.setItem("user",JSON.stringify(user));
                      this.$message.success(res.message+",请重新登陆");
                      this.$router.push("/login");
                      this.load(1);
                    }else {
                      this.$message.error(res.message);
                      this.load(1);
                    }
                  }).catch(res => {
                    this.$message.error(res.code);
                    this.load(1);
                  })
                }else if (user.identity==="教师"){
                  updateTeacher(user).then(res =>{
                    if (res.code===0){
                      sessionStorage.setItem("user",JSON.stringify(user));
                      this.$message.success(res.message+",请重新登陆");
                      this.$router.push("/login");
                      this.load(1);
                    }else {
                      this.$message.error(res.message);
                      this.load(1);
                    }
                  }).catch(res => {
                    this.$message.error(res.code);
                    this.load(1);
                  })
                }else if (user.identity==="管理员"){
                  updateAdministrator(user).then(res =>{
                    if (res.code===0){
                      sessionStorage.setItem("user",JSON.stringify(user));
                      this.$message.success(res.message+",请重新登陆");
                      this.$router.push("/login");
                      this.load(1);
                    }else {
                      this.$message.error(res.message);
                      this.load(1);
                    }
                  }).catch(res => {
                    this.$message.error(res.code);
                    this.load(1);
                  })
                }
              }else {
                this.$message.error("新密码与旧密码不能一致");
                this.load(0);
              }
            }else {
              this.$message.error("新密码与确认密码不一致");
              this.load(0);
            }
          }else{
            this.$message.error("旧密码错误");
            this.load(1);
          }
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-main{
  width: 100%;
  min-height: calc(100vh - 122px);
  display:flex;
  align-items: center;/*垂直居中*/
  justify-content: center;/*水平居中*/
  .changePassword_box{
    padding-top: 50px;
    width: 450px;
    height: 300px;
    border-radius: 15px;
    box-shadow: 0 0 10px #ddd;
    display: flex;
    justify-content: center;
  }
}
</style>